package com.hxzy.controller.system;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.hxzy.common.core.controller.BaseController;
import com.hxzy.common.core.domain.R;
import com.hxzy.common.core.domain.entity.CmsContent;
import com.hxzy.common.enums.AckCode;
import com.hxzy.system.domain.dto.CmsContentDTO;
import com.hxzy.system.service.CmsContentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Objects;

@RestController
@RequestMapping(value = "/api")
@Api(tags = "文章管理")
public class CmsContentController extends BaseController {

    @Autowired
    private CmsContentService cmsContentService;

    @ApiOperation(value = "文章管理分页查询")
    @GetMapping(value = "/cms/content/list")
    public R search(CmsContentDTO cmsContentDTO){
        System.out.println(cmsContentDTO);
        IPage<CmsContent> page = this.cmsContentService.search(cmsContentDTO);
        return super.pageVO(page);
    }

    /**
     * 新增文章管理
     */
    @ApiOperation(value = "新增文章管理")
    @PostMapping(value = "/cms/content/add")
    public R add(@Validated @RequestBody CmsContent cmsContent) {
        System.out.println(cmsContent.getCmsCategory().getId());
        boolean save = this.cmsContentService.seave(cmsContent);
        return save? R.ok(): R.build(AckCode.FAIL);
    }

    /**
     * 根据主键查询文章管理
     */
    @ApiOperation(value = "根据主键查询")
    @ApiImplicitParam(name = "id",value = "主键")
    @GetMapping(value = "/cms/content/{id}")
    public R  findById( @PathVariable(value = "id") Long id){
        CmsContent  cmsContent=this.cmsContentService.findById(id);
        if(Objects.isNull(cmsContent)){
            return R.build(AckCode.NOT_FOUND_DATA);
        }
        cmsContent.setUpdateTime(null);
        cmsContent.setCreateTime(null);

        return R.okHasData(cmsContent);
    }

    /**
     * 修改文章管理
     */
    @ApiOperation(value = "修改文章管理")
    @PutMapping(value = "/cms/content/update")
    public R update(@Validated @RequestBody CmsContent cmsContent) {
        boolean saved=this.cmsContentService.edit(cmsContent);
        return saved? R.ok(): R.build(AckCode.FAIL);
    }

    @ApiOperation("删除文章管理")
    @DeleteMapping("/cms/del/{ids}")
    public R delete(@PathVariable Long[] ids) {
        ArrayList<CmsContent> cmsContents = new ArrayList<>();
        for (Long id : ids) {
            // 判断该文章是否存在
            CmsContent cmsContent = this.cmsContentService.getById(id);
            if (ObjectUtil.isNotNull(cmsContent)) {
                cmsContent.setDelFlag("2");
                cmsContents.add(cmsContent);
            }
        }

        boolean batch = this.cmsContentService.updateBatchById(cmsContents);
        return batch ? R.ok() : R.build(AckCode.FAIL);

    }
}
